﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using MergeAndExecute._0;

namespace FilesMerger2._0
{
    static class Extensions
    {
        public static void DataTableToExcel(this DataTable table, String separator, Boolean includeHeaders, String path, String encoding, String sheetName, List<RowFormat> rowFormats = null)
        {
            if (table == null)
                return;
            var xmlDocument = new XmlDocument();
            if (File.Exists(path))
            {
                try
                {
                    xmlDocument.Load(path);
                }
                catch { }
            }
            for (Int32 i = 0; i < table.Rows.Count; i++)
            {
                DataRow row = table.Rows[i];
                ExcelGenerator.AppendRow(xmlDocument, encoding, sheetName, rowFormats.Count > i ? rowFormats[i] : null, row.ItemArray);
            }
            xmlDocument.Save(path);
            //File.AppendAllText(path, xmlDocument.OuterXml);
        }

        public static void AppendToExcel(this String source, String path, String encoding, String sheetName, RowFormat rowFormat = null)
        {
            var xmlDocument = new XmlDocument();
            if (File.Exists(path))
            {
                try
                {
                    xmlDocument.Load(path);
                }
                catch { }
            }
            ExcelGenerator.AppendRow(xmlDocument, encoding, sheetName, rowFormat, source);
            xmlDocument.Save(path);
        }

        public static void InvokeSafeAction(this Control control, Control root, Action action)
        {
            if (control.InvokeRequired)
            {
                root.Invoke(new Form1.InvokeSafe(InvokeSafeAction), control, root, action);
            }
            else
            {
                action();
            }
        }

        public static void AppendText(this RichTextBox box, string text, Color? color)
        {
            if (color.HasValue)
            {
                box.SelectionStart = box.TextLength;
                box.SelectionLength = 0;

                box.SelectionColor = color.Value;
                box.AppendText(text);
                box.SelectionColor = box.ForeColor;
            }
            else
            {
                box.AppendText(text);
            }
        }

        public static String ToHexString(this Color color)
        {
            return string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
        }

        public static Boolean AreEqual(this List<Int32> first, List<Int32> second  )
        {
            if (first.Count != second.Count)
                return false;
            for (Int32 i = 0; i < first.Count; i++)
                if (first[i] != second[i])
                    return false;
            return true;
        }


    }
}
